package com.bike.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bike.controller.BaseController;
import com.bike.entity.SysMenu;
import com.bike.entity.SysRole;
import com.bike.util.PageData;
import com.bike.service.impl.SysMenuServiceImpl;
import com.bike.service.impl.SystemServiceImpl;

 
@Controller
@RequestMapping(value="/sysMenu")
public class SysMenuController extends BaseController {
	
		@Autowired
		private SysMenuServiceImpl  sysMenuService;
		@Autowired
		private SystemServiceImpl systemService;
		@Autowired
		private SysMenuServiceImpl sysMenuServiceImpl;
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/system/menu/sysMenu_list";
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysMenuService.selectSysMenuList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysMenuService.selectSysMenuList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			if(pd.getLong("menuId")>0){
				model.addAttribute("pd", sysMenuService.selectBySysMenuId(pd.getLong("menuId")));
			} 
			model.addAttribute("menuTree", systemService.selectMenuByParentId(0));
			return "/system/menu/sysMenu_edit";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,SysMenu sysMenu){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				sysMenuService.saveOrUpdate(pd,sysMenu);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("menuId")>0){
					sysMenuService.deleteSysMenuById(pd.getLong("menuId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
	 	/**
		 * 详细信息
		 */
		@RequestMapping("detail")
		@ResponseBody
		public SysMenu detail(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysMenuService.selectBySysMenuId(pd.getLong("menuId"));
		}
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toMenu")
		public String toMenu(HttpServletRequest request,Model model){
			model.addAttribute("pd", PageData.getPageDataByPage(request));
			return "/system/role/treeMenu_list";
		}
		/**
		 * 根据父id获取菜单集合
		 * @param parentId
		 * @return
		 */
		@RequestMapping("menu")
		@ResponseBody
		public List<SysMenu> menu(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			long parentId=pd.getLong("parentId");
			if(pd.get("parentId")==null){
				parentId=pd.getLong("id");
			}
			long roleId=pd.getLong("roleId");
			if(roleId==0){
				List<SysMenu> menuList=systemService.selectMenuByParentId(parentId);
				return menuList;
			}else{
				List<SysMenu> menuList=sysMenuServiceImpl.selectBySysMenuTree(pd);
				return menuList ;
			}
			
		}
		/**
		 * 树形组件
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("choosetree")
		@ResponseBody
		public  List<SysMenu> choosetree(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			SysRole loginRole=(SysRole) request.getSession().getAttribute("role");
			return sysMenuService.selectBySysMenuTreeForRole(pd,loginRole);
		}
		/**
		 * 树形组件
		 * @param request
		 * @param model
		 * @return
		 */
		@RequestMapping("tree")
		@ResponseBody
		public  List<SysMenu> tree(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			SysRole role=(SysRole) request.getSession().getAttribute("role");
			pd.put("roleId", role.getRoleId());
			return sysMenuService.selectBySysMenuTree(pd);
		}
}
